<template>
  <div>
    <v-row>
      <v-col cols="12" md="2">
        <v-btn color="#2196f3" dark @click="dialogFormVisible = true">新增通知</v-btn>
      </v-col>
    </v-row>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      :data="announceList"
      border
      style="width: 100%"
    >
      <el-table-column fixed prop="title" label="标题" width="300"></el-table-column>
      <el-table-column prop="content" label="内容" width="350"></el-table-column>
      <el-table-column prop="url" label="链接" width="250"></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="deleteAnnounce(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="新增通知" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="链接(选填)">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <v-btn color="#fff" tile @click="dialogFormVisible = false">取 消</v-btn>
        <v-btn color="#2196f3" dark @click="beforeaddAnnounce('form')">确 定</v-btn>
      </div>
    </el-dialog>

    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="page"
      :total="count"
      :page-size="page_size"
      @current-change="handleCurrent"
    ></el-pagination>
  </div>
</template>

<script>
import {
  getAnnounceList,
  deleteAnnounce,
  addAnnounce,
} from "@/api/adminSerivce";
export default {
  data() {
    return {
      dialogFormVisible: false,
      loading: true,
      announceList: [],
      page: 1,
      page_size: 10,
      count: 0,
      form: {
        title: "",
        content: "",
        Url: "",
      },
      formId: {
        id: "",
      },
      searchForm: {
        keyWord: "",
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  methods: {
    _getAnnounceList() {
      getAnnounceList(this.page, this.page_size).then((res) => {
        if (res.data.code === 200) {
          if (this.count == 0) {
            //只有查询第一页才返回总数
            this.count = res.data.data.count;
          }
          this.announceList = res.data.data.data;
          this.loading = false;
        }
      });
    },
    handleCurrent(page) {
      this.page = page;
      this._getAnnounceList();
    },
    deleteAnnounce(row) {
      this.formId.id = row.aid;
      deleteAnnounce(this.formId).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("删除成功");
          //重新获取
          this._getAnnounceList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    beforeaddAnnounce(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid) {
          this.addAnnounce();
        } else {
          this.$message.error("请检查输入的数据");
          return;
        }
      });
    },
    addAnnounce() {
      this.dialogFormVisible = false;
      addAnnounce(this.form)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success("发送成功");
            //重新获取
            this._getAnnounceList();
          }
        })
        .catch((err) => {
          this.$message.error(err.response.data.msg);
        });
      this.form.title = "";
      this.form.content = "";
      this.form.Url = "";
    },
    search() {
      this.$message("此功能开发中");
    },
  },
  created() {
    this._getAnnounceList();
  },
};
</script>
